package com.bogewang.weixin.mp.handler;

import com.bogewang.core.base.BaseResponse;
import com.bogewang.core.constants.Constants;
import com.bogewang.core.core.utils.RedisUtil;
import com.bogewang.core.core.utils.RegexUtils;
import com.bogewang.member.entity.UserDTO;
import com.bogewang.weixin.feign.MemberServiceFeign;
import com.bogewang.weixin.mp.builder.TextBuilder;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;

import java.util.Map;

import static me.chanjar.weixin.common.api.WxConsts.XmlMsgType;

/**
 * @author Binary Wang(https://github.com/binarywang)
 */
@Component
@Slf4j
@ComponentScan(basePackages = {"com.bogewang"})
@SuppressWarnings("static-access")
public class MsgHandler extends AbstractHandler {

    // 用户发送手机验证码提示
    @Value("${bogewang.weixin.registration.code.message}")
    private String registrationCodeMessage;
    // 默认用户发送验证码提示
    @Value("${bogewang.weixin.default.registration.code.message}")
    private String defaultRegistrationCodeMessage;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private MemberServiceFeign memberServiceFeign;


    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService weixinService,
                                    WxSessionManager sessionManager) {

        if (!wxMessage.getMsgType().equals(XmlMsgType.EVENT)) {
            // TODO 可以选择将消息保存到本地
        }

        // 当用户输入关键词如“你好”，“客服”等，并且有客服在线时，把消息转发给在线客服
        try {
            if (StringUtils.startsWithAny(wxMessage.getContent(), "你好", "客服")
                    && weixinService.getKefuService().kfOnlineList().getKfOnlineList().size() > 0) {
                return WxMpXmlOutMessage.TRANSFER_CUSTOMER_SERVICE().fromUser(wxMessage.getToUser())
                        .toUser(wxMessage.getFromUser()).build();
            }
        } catch (WxErrorException e) {
            e.printStackTrace();
        }

        // TODO 组装回复消息
        // 1.验证关键字是否为手机号码类型
        String fromContent = wxMessage.getContent();

        if (RegexUtils.checkMobile(fromContent)) {
            // 2.校验手机号码是否存在;
            BaseResponse<UserDTO> userEntityBaseResponse = new BaseResponse<UserDTO>();
            try {
                userEntityBaseResponse = memberServiceFeign.existMobile(fromContent);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (userEntityBaseResponse.getRtnCode().equals(Constants.HTTP_RES_CODE_200)) {
                return new TextBuilder().build("该手机号码" + fromContent + "已经注册过了!", wxMessage, weixinService);
            }
            if (!userEntityBaseResponse.getRtnCode().equals(Constants.HTTP_RES_CODE_203)) {
                return new TextBuilder().build(userEntityBaseResponse.getRtnMsg(), wxMessage, weixinService);
            }

            // 如果发送消息为手机号码类型,则发送短信验证码
            int registCode = registCode();
            String retContext = String.format(registrationCodeMessage, registCode);
            // 放入缓存;
            redisUtil.setString(Constants.WEIXINCODE_KEY + fromContent, registCode + "", Constants.WEIXINCODE_TIMEOUT);
            return new TextBuilder().build(retContext, wxMessage, weixinService);

        }
        return new TextBuilder().build(defaultRegistrationCodeMessage, wxMessage, weixinService);

    }

    // 获取注册码
    private int registCode() {
        int registCode = (int) (Math.random() * 9000 + 1000);
        return registCode;
    }

}
